{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d3ea4855-169a-4f49-855b-f2484076a548",
   "metadata": {},
   "source": [
    "## 1. 环境检查\n",
    "\n",
    "这个Notebook主要用于确保开发环境正确配置，特别是针对使用PyTorch和YOLO进行计算机视觉任务的环境。它检查了Python和PyTorch的版本，验证了CUDA的可用性，并确保YOLO库是最新版本。这对于任何涉及深度学习和计算机视觉的项目都是至关重要的步骤。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "986ee1a2-f454-4516-b818-22cee58db556",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python 3.11.5\n"
     ]
    }
   ],
   "source": [
    "# python 版本\n",
    "!python --version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "29d862fa-7060-4e11-9fbe-9e84b462eb72",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PyTorch version: 2.1.2+cpu\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "os.environ['KMP_DUPLICATE_LIB_OK']='True'\n",
    "\n",
    "import torch\n",
    "# 获取 PyTorch 版本\n",
    "print(f\"PyTorch version: {torch.__version__}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "77e3cc84-8772-4334-a72e-79ef88a65237",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CUDA is not available. PyTorch will use CPU.\n",
      "Current device: cpu\n"
     ]
    }
   ],
   "source": [
    "# 检查 CUDA 是否可用\n",
    "if torch.cuda.is_available():\n",
    "    device = \"cuda\"\n",
    "    print(\"CUDA is available. PyTorch can use GPU.\")\n",
    "elif torch.backends.mps.is_available():\n",
    "    device = \"mps\"\n",
    "    print(\"MPS is available. PyTorch can use CPU.\")\n",
    "else:\n",
    "    device = \"cpu\"\n",
    "    print(\"CUDA is not available. PyTorch will use CPU.\")\n",
    "\n",
    "# 查看当前设备\n",
    "# device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "print(f\"Current device: {device}\")\n",
    "# 查看 GPU 信息\n",
    "if torch.cuda.is_available():\n",
    "    print(f\"Number of GPUs: {torch.cuda.device_count()}\")\n",
    "    print(f\"Current GPU: {torch.cuda.current_device()}\")\n",
    "    print(f\"Name of the current GPU: {torch.cuda.get_device_name(torch.cuda.current_device())}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4890331e-8f6b-48c1-9b6f-dd503ec85af9",
   "metadata": {},
   "source": [
    "## 验证YOLO安装成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "db2d0208-542e-44ea-86b4-25aee82cbf9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8.3.32\n"
     ]
    }
   ],
   "source": [
    "from ultralytics import YOLO\n",
    "import ultralytics\n",
    "\n",
    "print(ultralytics.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "23fe13b9-4a26-445b-a307-ff9d48fc8024",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: ultralytics in d:\\programdata\\anaconda3\\lib\\site-packages (8.3.37)\n",
      "Collecting ultralytics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  ERROR: HTTP error 403 while getting https://pypi.tuna.tsinghua.edu.cn/packages/6f/e0/63760d3bfe6a0ace29ea4e8ac7a83c5318fc7b5f4043b0dad1cddb470877/ultralytics-8.3.38-py3-none-any.whl (from https://pypi.tuna.tsinghua.edu.cn/simple/ultralytics/) (requires-python:>=3.8)\n",
      "ERROR: Could not install requirement ultralytics from https://pypi.tuna.tsinghua.edu.cn/packages/6f/e0/63760d3bfe6a0ace29ea4e8ac7a83c5318fc7b5f4043b0dad1cddb470877/ultralytics-8.3.38-py3-none-any.whl because of HTTP error 403 Client Error: Forbidden for url: https://pypi.tuna.tsinghua.edu.cn/packages/6f/e0/63760d3bfe6a0ace29ea4e8ac7a83c5318fc7b5f4043b0dad1cddb470877/ultralytics-8.3.38-py3-none-any.whl for URL https://pypi.tuna.tsinghua.edu.cn/packages/6f/e0/63760d3bfe6a0ace29ea4e8ac7a83c5318fc7b5f4043b0dad1cddb470877/ultralytics-8.3.38-py3-none-any.whl (from https://pypi.tuna.tsinghua.edu.cn/simple/ultralytics/) (requires-python:>=3.8)\n"
     ]
    }
   ],
   "source": [
    "# 升级 YOLO\n",
    "!pip install ultralytics -U"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4876f0f1-a509-40c8-9304-b5f5559bb54c",
   "metadata": {},
   "source": [
    "## 环境统一"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef896cb1-2948-4588-9790-4939eb933dd8",
   "metadata": {},
   "source": [
    "- python 3.10\n",
    "- torch 2.x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04707185-7360-48d3-aa81-2d2d2101cc01",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
